﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OnlineBookReader;
using HtmlExtractor;
using SLHtmlExtractor.ObjectOutput;
using Reader.Domain;
using System.Xml.Serialization;

namespace Reader.View
{
    [Sample("目录", "content")]
    public partial class ContentView : OnlineObjectView
    {
        public ContentView()
        {
            InitializeComponent();
        }
        public ContentView(string uri):this()
        {
            Uri = uri;
        }
        string Uri;
        protected override object GenerateObject(string html, string uri)
        {
            return SaveFile.GenerateObject<BookContent>(html, uri);
        }
        protected override void SetElement(object o)
        {
            BookContent bp = o as BookContent;
           // this.DataContext = bp;
            if (null != bp)
            {
                if (bp.Chapters != null)
                {
                    bookName.Text = bp.BookName;
                    authorName.Text = bp.Author;
                    bookPage.CommandParameter = bp.BookPage;
                    bookPage.Visibility = Visibility.Visible;
                    listBox1.DataContext = bp.Chapters;
                }
                if (bp.VipChapters != null)
                {
                    vip.Visibility = Visibility.Visible;
                    vipChapters.DataContext = bp.VipChapters;
                }
            }
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadObject(Uri, typeof(BookContent));
            ScrollViewer SampleScroller = ((ScrollViewer)((ContentPresenter)(VisualTreeHelper.GetParent(this))).Parent);
            SampleScroller.KeyUp += new KeyEventHandler(SampleScroller_KeyUp);
            SampleScroller.UpdateLayout();
            SampleScroller.ScrollToTop();
            SampleScroller.Focus();
        }
       
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hb = e.OriginalSource as HyperlinkButton;
            SampleBrowser.Current.PopulateSamplesTab("chapter", hb.CommandParameter.ToString());
        }

        void SampleScroller_KeyUp(object sender, KeyEventArgs e)
        {
            ScrollViewer SampleScroller = sender as ScrollViewer;
            
            if (e.Key == Key.Home)
            {
                SampleScroller.UpdateLayout();
                SampleScroller.ScrollToTop();
            }
            if (e.Key == Key.End)
            {
                SampleScroller.UpdateLayout();
                SampleScroller.ScrollToBottom();
            }
        }

        private void bookPage_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hb = e.OriginalSource as HyperlinkButton;
            SampleBrowser.Current.PopulateSamplesTab("bookpage", hb.CommandParameter.ToString());
        }
    }
}
